"""
  Scoruino - Scoringboard for the arduino

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
import os
avr = Environment()
avr['BUILDERS']['Flash'] = Builder(action='util/reset_board.py ${SERIAL_PORT} && $AVRDUDE $AVRDUDE_OPTS -Uflash:w:$SOURCE')
avr['BUILDERS']['HexFile'] = Builder(action='$OBJCOPY -O ihex -R .eeprom $SOURCE $TARGET', suffix='.hex')
avr['AVRDUDE'] = "${ARDUINO_INSTALL}/hardware/tools/avrdude"
avr['AVRDUDE_OPTS'] = "-C ${ARDUINO_INSTALL}/hardware/tools/avrdude.conf -pm1280 -cstk500v1 -P${SERIAL_PORT} -b57600 -D"
avr['ARDUINO_INSTALL'] = '/usr/local/arduino-0015/'
avr['SERIAL_PORT'] = '/dev/ttyUSB0'
avr['OBJCOPY'] = 'avr-objcopy'
avr['CC'] = 'avr-gcc'
avr['AR'] = 'avr-ar'
avr['CXX'] = 'avr-g++'
avr['F_CPU'] = 16000000
avr['ARDUINO'] = '${ARDUINO_INSTALL}/hardware/cores/arduino/'
avr['CPATH'] = [
        '#${SRCDIR}/include',
        '/usr/lib/avr/include',
        '${ARDUINO}',
        '#${SRCDIR}/libs/include',
    ]
avr['CPPPATH'] = '${CPATH}'
avr['CPU'] = 'atmega1280'
avr['LINKFLAGS'] = '-mmcu=${CPU} -Os'
avr['CFLAGS'] = [
        '-mmcu=${CPU}',
        '-DF_CPU=${F_CPU}',
        '-Wall',
        '-Wextra',
        '-gstabs',
        '-Os',
#        '-funsigned-char',
#        '-funsigned-bitfields',
#        '-fpack-struct',
#        '-fshort-enums',
    ]
avr['CXXFLAGS'] = '${CFLAGS}'
Export("avr")


libs = SConscript('libs/SConscript')
target = 'scoruino.elf'
src = Split('scoruino.cpp')

objects = avr.Object(src)
extra_objects = []
program = avr.Program(target, objects + extra_objects, LIBS=libs)

hexfile = avr.HexFile(program)
avr.Flash(hexfile)

